package com.zh.zcy.config;

import com.zh.zcy.filter.MyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfig {

    @Autowired
    private MyFilter myFilter;  // 直接注入 Spring 托管的过滤器实例，才能从yml文件读取白名单

    //注册过滤器到容器
    @Bean
    public FilterRegistrationBean<MyFilter> authFilter() {
        //这里的泛型可能会提示报错
        FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(myFilter);
        // 拦截所有请求到拦截器中，根据filterChain.doFilter(request,response);决定是否放行
        registration.addUrlPatterns("/*");
        //这里指定启动顺序1，才能使init方法在项目启动时被调用，不指定只有在第一个请求到达时调用
        //数字越小，执行顺序越靠前
        registration.setOrder(1);
        return registration;
    }
}
